<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close :close-on-press-escape="mode != 'show' ? false : true" @closed="$emit('closed')" align-center :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px">
            <el-link type="primary" style="margin-bottom: 15px;" @click="downloadTemplate">下载导入模版</el-link>
            <sc-upload-file v-model="form.file" :limit="1" tip="最多上传1个文件,单个文件不要超过10M,请上传.xls/.xlsx格式文件">
                <el-button type="primary" size="small" icon="el-icon-upload">上传附件</el-button>
            </sc-upload-file>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	props: {
		apiObj: {
            type: Object, default: () => {}
		},
        base:{
            type: String, default: ''
        }
	},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '导入',
				edit: '导入',
				show: '导入'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				file:"",
			},
			//验证规则
			rules: {
				status: [
					{required: true, message: '请选择审核状态'}
				],
			},
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.apiObj.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
                        this.$emit('success', res.data)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},

        downloadTemplate(){
            const fileUrl = this.base;
            const link = document.createElement("a");
            link.href = fileUrl;
            link.download = fileUrl;
            link.click();
        },
	}
}
</script>

<style lang="scss">
.el-input-group__append {
	padding: 0 8px;
}
</style>
